import { ref } from 'vue'

export function usePageLayout() {
	const statusBarHeight = ref(0)
	const navHeight = ref(44)
	const contentHeight = ref(0)
	const contentWidth = ref(0)
	const topInsets = ref(0)
	const bottomInsets = ref(0)
	const scale = ref(1) // ✅ 新增比例

	const updateLayout = (top : boolean = false, bottom : boolean = false) => {
		const windowInfo = uni.getWindowInfo()
		const safeAreaInsets = windowInfo.safeAreaInsets
		statusBarHeight.value = safeAreaInsets?.top ?? 0
		topInsets.value = top ? navHeight.value + statusBarHeight.value : 0
		bottomInsets.value = bottom ? safeAreaInsets?.bottom == 0 ? 58 : safeAreaInsets?.bottom + 38 : safeAreaInsets?.bottom == 0 ? 24 : 34

		let deduction = 0
		if (top) deduction += navHeight.value + statusBarHeight.value
		if (bottom) deduction += bottomInsets.value

		contentWidth.value = windowInfo.screenWidth
		contentHeight.value = windowInfo.screenHeight - deduction

		scale.value = windowInfo.screenWidth / 375.0 // ✅ 计算比例

		console.log('[usePageLayout] windowInfo=', windowInfo)
		console.log('navHeight:', navHeight.value)
		console.log('statusBarHeight:', statusBarHeight.value)
		console.log('bottomInsets:', bottomInsets.value)
		console.log('contentWidth:', contentWidth.value)
		console.log('contentHeight:', contentHeight.value)
		console.log('scale screenWidth / 375:', scale.value)
	}

	return {
		statusBarHeight,
		navHeight,
		contentWidth,
		contentHeight,
		topInsets,
		bottomInsets,
		scale,
		updateLayout
	}
}